<template>
  <div class="container">
    <a-form>
      <a-row :gutter="16">
        <a-col :span="10">
          <a-form-item label="钢类名称">
            <a-input allow-clear v-model="state.sd.cname" placeholder="" />
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-space>
            <a-button type="primary" @click="searchDuanZao">
              <template #icon><icon-search /></template>查询
            </a-button>
            <a-button type="primary" @click="addDuanZao">
              <template #icon><icon-plus-circle /></template>添加
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-form>
    <a-table :bordered="{cell:true}" :pagination="false" stripe :data="state.pageObj.list">
      <template #columns>
        <a-table-column title="钢类名称" data-index="name"></a-table-column>
        <a-table-column title="基础锻造费" data-index="chengben"></a-table-column>
        <a-table-column title="操作" width="350">
          <template #cell="{ record }">
            <a-space>
              <a-button type="primary" size="small" @click="editDuanZao(record.id)">
                <template #icon><icon-edit /></template>修改
              </a-button>
              <a-popconfirm content="确认删除吗?" @ok="deleteDuanZao(record.id)">
                <a-button type="primary" status="danger" size="small">
                  <template #icon><icon-delete /></template>删除
                </a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>
    <a-pagination class="mt10" show-total :total="state.pageObj.total" @change="loadDuanZao" @page-size-change="loadDuanZao" v-model:current="state.sd.pageNum" v-model:page-size="state.sd.pageSize"></a-pagination>
    <a-modal :title="state.popupTitle" v-model:visible="state.entityDetailVisible" :footer="false" width="888px">
      <DuanZaoDetail v-if="state.entityDetailVisible" v-model="state.entityDetailVisible" :entityId="state.popupEntityId"></DuanZaoDetail>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import { Message } from '@arco-design/web-vue'
import mittBus from "@/utils/route-listener"
import {reactive, ref, onMounted, onUnmounted, defineAsyncComponent} from "vue";
import PermissionType from "@/utils/permissionType";
const DuanZaoDetail = defineAsyncComponent(() => import('@/views/duanzao/duanzaoDetail.vue'));

const state = reactive({
  permissionType:PermissionType,
  sd: {
    cname:'',
    pageNum: 1,
    pageSize: 15
  },
  pageObj: {},
  entityDetailVisible : false,
  popupTitle : '',
  popupEntityId : 0,
})

const searchDuanZao = ()=>{
  state.sd.pageNum = 1;
  loadDuanZao();
}
const loadDuanZao = ()=>{
  axios.post('/duanzao/pageduanzao', state.sd).then(res => {
    const { data } = res
    state.pageObj = data
  })
}
const addDuanZao = ()=>{
  state.popupEntityId = 0;
  state.popupTitle= "新增钢类锻造基础费"
  state.entityDetailVisible= true
}
const editDuanZao = (id)=>{
  state.popupEntityId= id;
  state.popupTitle= "修改钢类锻造基础费"
  state.entityDetailVisible= true
}
const deleteDuanZao = (id)=>{
  axios.post('/duanzao/deleteduanzao',{id : id}).then(res => {
    const { code, msg } = res
    if(code === '200') {
      Message.success(msg)
      loadDuanZao();
    }
  })
}
onMounted(()=>{
  loadDuanZao();
  mittBus.on("dzadd", ()=>{
    state.sd.pageNum = 1;
    loadDuanZao();
  })
  mittBus.on("dzedit", ()=>{
    loadDuanZao();
  })
})
onUnmounted(()=>{
  mittBus.off("dzadd", ()=>{})
  mittBus.off("dzedit",()=>{})
})
</script>

<style scoped>

</style>
